{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Miniconda\\envs\\geo\\lib\\site-packages\\numpy\\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:\n",
      "d:\\Miniconda\\envs\\geo\\lib\\site-packages\\numpy\\.libs\\libopenblas.FB5AE2TYXYH2IJRDKGDGQ3XBKLKTF43H.gfortran-win_amd64.dll\n",
      "d:\\Miniconda\\envs\\geo\\lib\\site-packages\\numpy\\.libs\\libopenblas64__v0.3.21-gcc_10_3_0.dll\n",
      "  warnings.warn(\"loaded more than 1 DLL from .libs:\"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "ename": "UnidentifiedImageError",
     "evalue": "cannot identify image file <_io.BytesIO object at 0x000001EDE110E630>",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnidentifiedImageError\u001b[0m                    Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 13\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[38;5;28mprint\u001b[39m(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mexists(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/data/models/detr-resnet-50\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m     12\u001b[0m url \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttp://images.cocodataset.org/val2017/000000039769.jpg\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m---> 13\u001b[0m image \u001b[38;5;241m=\u001b[39m \u001b[43mImage\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mopen\u001b[49m\u001b[43m(\u001b[49m\u001b[43mrequests\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstream\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraw\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     15\u001b[0m \u001b[38;5;66;03m# # you can specify the revision tag if you don't want the timm dependency\u001b[39;00m\n\u001b[0;32m     16\u001b[0m processor \u001b[38;5;241m=\u001b[39m DetrImageProcessor\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/data/models/detr-resnet-50-train\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\Miniconda\\envs\\geo\\lib\\site-packages\\PIL\\Image.py:3339\u001b[0m, in \u001b[0;36mopen\u001b[1;34m(fp, mode, formats)\u001b[0m\n\u001b[0;32m   3337\u001b[0m     warnings\u001b[38;5;241m.\u001b[39mwarn(message)\n\u001b[0;32m   3338\u001b[0m msg \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcannot identify image file \u001b[39m\u001b[38;5;132;01m%r\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (filename \u001b[38;5;28;01mif\u001b[39;00m filename \u001b[38;5;28;01melse\u001b[39;00m fp)\n\u001b[1;32m-> 3339\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m UnidentifiedImageError(msg)\n",
      "\u001b[1;31mUnidentifiedImageError\u001b[0m: cannot identify image file <_io.BytesIO object at 0x000001EDE110E630>"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
      "\u001b[1;31m请查看单元格中的代码，以确定故障的可能原因。\n",
      "\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
      "\u001b[1;31m有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "from transformers import DetrImageProcessor, DetrForObjectDetection,AutoModelForObjectDetection\n",
    "from transformers import AutoTokenizer, AutoModelForQuestionAnswering,Trainer,TrainingArguments\n",
    "from transformers.models.detr.modeling_detr import DetrConvEncoder # 用於定位源碼\n",
    "from transformers import DetrConfig\n",
    "import torch\n",
    "from PIL import Image\n",
    "import requests\n",
    "import os\n",
    "# https://blog.csdn.net/weixin_44826203/article/details/137605207\n",
    "print(os.path.exists(\"/data/models/detr-resnet-50\"))\n",
    "\n",
    "url = \"http://images.cocodataset.org/val2017/000000039769.jpg\"\n",
    "image = Image.open(requests.get(url, stream=True).raw)\n",
    "\n",
    "# # you can specify the revision tag if you don't want the timm dependency\n",
    "processor = DetrImageProcessor.from_pretrained(\"/data/models/detr-resnet-50-train\")\n",
    "inputs = processor(images=image, return_tensors=\"pt\")\n",
    "# print(inputs.keys())\n",
    "model = DetrForObjectDetection.from_pretrained(pretrained_model_name_or_path=\"/data/models/detr-resnet-50\", revision=\"no_timm\")\n",
    "print(\"pretrain model loading finish\")\n",
    "\n",
    "configuration = DetrConfig()\n",
    "configuration.num_labels = 1000\n",
    "# print(model)\n",
    "##################################\n",
    "new_model = DetrForObjectDetection(configuration)\n",
    "# 获取两个模型的state_dict  \n",
    "state_dict_src = model.state_dict()  \n",
    "print(\"原始model \",[k for k, _ in state_dict_src.items()])\n",
    "\n",
    "state_dict_tgt = new_model.state_dict() \n",
    "\n",
    "# 假设我们只想要加载fc1层的参数  \n",
    "# params_to_load = {k: v for k, v in state_dict_src.items() if not k.startswith('class_labels_classifier')}  \n",
    "# # 更新目标模型的state_dict  \n",
    "# state_dict_tgt.update(params_to_load)  \n",
    "# # 加载更新后的state_dict到目标模型  \n",
    "# new_model.load_state_dict(state_dict_tgt)  \n",
    "\n",
    "\n",
    "# outputs = new_model(**inputs)\n",
    "# print(outputs.keys())\n",
    "# print(outputs['logits'].shape)\n",
    "# print(outputs['pred_boxes'].shape)\n",
    "# print(outputs['last_hidden_state'].shape)\n",
    "# print(outputs['encoder_last_hidden_state'].shape)\n",
    "# target_sizes = torch.tensor([image.size[::-1]])\n",
    "# results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]\n",
    "\n",
    "# for score, label, box in zip(results[\"scores\"], results[\"labels\"], results[\"boxes\"]):\n",
    "#     box = [round(i, 2) for i in box.tolist()]\n",
    "#     print(\n",
    "#             f\"Detected {model.config.id2label[label.item()]} with confidence \"\n",
    "#             f\"{round(score.item(), 3)} at location {box}\"\n",
    "#     )\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# new_model_directory = \"./merged_model_an\"\n",
    "# merged_model = model.merge_and_unload()\n",
    "# # 将权重保存为safetensors格式的权重, 且每个权重文件最大不超过2GB(2048MB)\n",
    "new_model_directory = '/data/models/detr-resnet-50-taibiao-merged'\n",
    "model.save_pretrained(new_model_directory, max_shard_size=\"2048MB\", safe_serialization=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
